package com.wing.sys.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wing.common.Constants;
import com.wing.common.util.GUID;
import com.wing.common.util.PageBean;
import com.wing.sys.dao.RoleUserDao;
import com.wing.sys.dao.UserDAO;
import com.wing.sys.domain.RoleUser;
import com.wing.sys.domain.User;
@Service
@Transactional
public class UserManager {
	@Autowired
	private UserDAO userdao;
	@Autowired
	private RoleUserDao roleUserDao;
	
	public List<Map<String,Object>> findAll(){
		return userdao.findAll();	 
	}
	
	public User findById(String id){
		return userdao.findById(id);	 
	}

	public List<Map<String,Object>> findUsersByOrgId(String orgId) {
		return userdao.findUsersByOrgId(orgId);
	}

	public PageBean findPageList(int currentPage, int pageSize, String seachv,String siteId) {
		return userdao.findPageList(currentPage, pageSize,seachv,siteId); 
	}
	
	public void saveUser(User user,String userId){
		if(user.getId() == null ||  "".equals(user.getId())){
			if(userId!=null){
				user.setId(userId);
			}else{
				user.setId(new GUID().toString());
			}
			user.setCreatetime((new Date()));
			userdao.insert(user);
		}else{
			User u = userdao.findById(user.getId());
			u.setBank_account(user.getBank_account());
			u.setEducation(user.getEducation());
			u.setEmail(user.getEmail());
			u.setMobile(user.getMobile());
			u.setName(user.getName());
			u.setOffice_phone(user.getOffice_phone());
			u.setOrder_num(user.getOrder_num());
			u.setPayment_unit(user.getPayment_unit());
			u.setRemarks(user.getRemarks());
			u.setSex(user.getSex());
			if(user.getLogin_code()!=null){
				u.setLogin_code(user.getLogin_code());
				u.setPassword(user.getPassword());
			}
			userdao.update(u);
		}
	}

	public void updateUser(User user){
		userdao.update(user);
	}
	
	public void deleteUser(String id){
		//删除USER的关联关系
		roleUserDao.deleteRoleUserByUserId(id);
		userdao.delete(id);
	}

	public void deleteUsers(String[] chk) {
		for(int i=0;i<chk.length;i++){
			deleteUser(chk[i]);
		}
	}

	public void addRolesForUser(String[] rolearray, String userid) {
		RoleUser ru = null;
		for(int i=0;i<rolearray.length;i++){
			if(rolearray[i].trim().equals("")){
				continue;
			}
			ru = new RoleUser();
			ru.setRoleid(rolearray[i]);
			ru.setUserid(userid);
			List list = roleUserDao.findRoleUser(ru);
			if(list.size() == 0){
				ru.setId(new GUID().toString());
				roleUserDao.insert(ru);
			}
		}
	}

	public void deleteRolesForUser(String[] rolearray, String userid) {
		RoleUser ru = null;
		for(int i=0;i<rolearray.length;i++){
			roleUserDao.deleteRoleUser(userid,rolearray[i]);
		}
	}

	public Map findUser(String login_ode, String password) {
		List<Map<String, Object>> list = userdao.findUser(login_ode,password);
		if(list.size()>0){
			return (Map)list.get(0);
		}
		return null;
	}
	
	public boolean isAdminByUserId(String userId){
		boolean isAdmin = false;
		PageBean pb = userdao.findPageRolesByUserId(1,1000,userId);
		List<Map<String, Object>> roleList = pb.getRecordList();
		for(int i = 0; i < roleList.size(); i++){
			if(roleList.get(i).get("id").equals(Constants.SYSTEM_SUPERADMIN_ROLEID))isAdmin= true;
		}
		return isAdmin;
	}


	public PageBean findPageRolesByUserId(int currentPage, int pageSize,
			String userId) {
		return userdao.findPageRolesByUserId(currentPage,pageSize,userId);
	}

	public List findUsersByLoginCode(String lgcode) {
		return userdao.findUsersByLoginCode(lgcode);
	}

	public PageBean findPageOrgsByUserId(int currentPage, int pageSize,
			String userId) {
		return userdao.findPageOrgsByUserId(currentPage,pageSize,userId);
	}
	
}
